<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script type="text/javascript" src="jquery-1.7.min.js"></script>
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript" src="chrome_ex_oauthsimple.js"></script>
<script type="text/javascript" src="chrome_ex_oauth.js"></script>
</head>
<body>
<script type="text/javascript">
var oauth=ChromeExOAuth.initBackgroundPage({
	'request_url': "http://api.t.sina.com.cn/oauth/request_token",
	'authorize_url': "http://api.t.sina.com.cn/oauth/authorize",
	'access_url': "http://api.t.sina.com.cn/oauth/access_token",
	'consumer_key':"3640942651",
	'consumer_secret': "2d5ce5527431c7ee3670f94de918d248",
	'scope': "",
	'app_name': "isina"
});	

var defaultOptions = "{\"hide\":false,\"draft\":false, \"bigPic\":false, \"nolimit\":false, \"ad\": false, \"sendToSina\": false, \"shortcut\":false}";
var defaultHide = "{\"admin\":{\"right\":[]}, \"profile\":{\"right\":[]}}";

if(getLocalStorageVal('options', '') == '')
{
  localStorage['options'] = defaultOptions;
}

if(getLocalStorageVal('hide', '') == '')
{
  localStorage['hide'] = defaultHide;
}
    chrome.extension.onRequest.addListener(onRequest);
    function onRequest(request, sender, callback) {
        switch(request.action)
        {
            case 'getMuteTags':
                var newMuteTags = JSON.parse(getLocalStorageVal('mute', '[]'));
                callback(newMuteTags);
                break;
            case "getOptions":
                var opt = {'options':JSON.parse(getLocalStorageVal("options")), 'hide':JSON.parse(getLocalStorageVal("hide"))};
                opt.muteTags = JSON.parse(getLocalStorageVal('mute', '[]'));
                getCookies('http://weibo.com', 'wvr', function(version){
                    opt.version = version;
                    callback(opt);
                });
                break;
            case "hide":
                var ele = request.ele;
                var hide = JSON.parse(getLocalStorageVal("hide"));
                switch(ele.pos)
                {
                    case 'right':
                        if(ele.admin)
                        {
                            hide.admin.right = $.merge(hide.admin.right, [ele.index]);
                        }
                        else
                        {
                            hide.profile.right = $.merge(hide.profile.right, [ele.index]);
                        }
                        break;
                }
                localStorage['hide'] = JSON.stringify(hide);
                callback(JSON.stringify(hide));
                break;
            case 'mute':
                var mute = JSON.parse(getLocalStorageVal('mute', '[]'));
                var muteTags = (request.tags || '').trim().split(' ');
                $.each(muteTags, function(i, val){
                    if(val.trim().length> 0 && $.inArray(val.trim(), mute) == -1)
                    {
                        mute.push(val.trim());
                    }
                });
                if(mute.length > 0){
                    localStorage['mute'] = JSON.stringify(mute);
                }
                
                callback('success');
                break;
        }
    }
    
        if(JSON.parse(getLocalStorageVal('options', defaultOptions)).sendToSina === true){
            chrome.contextMenus.create({"title": "用\"i辛辣微博\"分享", "contexts":["page","selection"], "onclick": createShadowbox});
        }
        
        function createShadowbox(info, tab) {
            chrome.tabs.getSelected(null, function(tab) {
                    
                    chrome.tabs.executeScript(tab.id, {file: "commonmyweibo.js"});
            });
                    
            chrome.tabs.getSelected(null, function(tab) {
            });
        }
        
        function getLocalStorageVal(key, defaultVal) {
        var val = window.localStorage[key];
        if (val == undefined || val == null) {
            return defaultVal;
        } else {
            return val;
        }
    };
    
            function sendStatus(status) {
              var url = 'http://api.t.sina.com.cn/statuses/update.json';
              var encode_status=encodeURIComponent(status);
              var request = {
                        'method': 'post',
                        'parameters':{
		                    'status':encode_status
	                        }
				        };
                if(!oauth.hasToken()){
                    var conf=confirm("授权给\"i辛辣微博\"Chrome插件?");
                    if(conf)
                    {
					    oauth.authorize(function(){
				            oauth.sendSignedRequest(url, onSentStatus, request);
				            });          
				     }
				}
				else
				{
				      oauth.sendSignedRequest(url, onSentStatus, request);
				}

      };
      
      function onSentStatus(response)
      {

      }
      
    function getCookies(domain, name, callback) {
        chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
            if(callback) {
                callback(cookie.value);
            }
        });
    }
</script>
</body>
</html>
